For Each...Next Statement

Loops through the elements of a one-dimensional array.


Syntax

For Each element [As datatype] In array

[statements]

Next

PartDescription
element A variable of the same data type as array that refers to an element of array. The loop processes each value in array.
datatype Optional: The datatype of the array element. It can be any one-dimensional array. If you declare the datatype with the optional AS clause, you do not have to do so with a Dim statement. The datatype must match array's datatype.
array A one-dimensional array.


Examples

The following function adds up the numbers in the array passed to it.

Function SumStuff(values() as Double) as Double
  Dim sum, element as Double
 For Each element In values
  sum=sum+element
 Next
Return Sum

To call the function, pass an array of doubles in a statement such as:

s=SumStuff(MyNumbers)

where s is declared as a Double and MyNumbers is an array of Doubles.


See Also

For...Next statement, Dim statement.